package org.team4.service.meta;

import java.util.Iterator;
import java.util.List;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.team4.constants.SQLConstant;
import org.team4.dao.DAOFactory;
import org.team4.dao.meta.RoleModuleDepartmentsDAO;
import org.team4.dao.meta.RoleModuleFieldDAO;
import org.team4.dao.meta.RoleModuleOperateDAO;
import org.team4.dao.meta.RoleModuleUsersDAO;
import org.team4.dao.meta.RoleUsersDAO;
import org.team4.dao.meta.ScopeDataVisibleDAO;
import org.team4.domain.RoleUsers;
import org.team4.exception.CRUDException;
import org.team4.service.IService;
import org.team4.util.CacheUtil;
import org.team4.util.Validate;
import org.team4.vo.RoleCache;


/**
 * 权限缓存服务类
 * 
 * @author MarkerKing
 *
 */
@Service
public class RoleCacheService implements IService {

    private RoleUsersDAO roleUsersDAO;
    private RoleModuleOperateDAO roleModuleOperateDAO;
    private RoleModuleFieldDAO roleModuleFieldDAO;
    private RoleModuleUsersDAO roleModuleUsersDAO;
    private RoleModuleDepartmentsDAO roleModuleDepartmentsDAO;
    private ScopeDataVisibleDAO scopeDataVisibleDAO;
    
    @Autowired
    public void setSessionFactory(SessionFactory sessionFactory) {
        roleUsersDAO = (RoleUsersDAO) DAOFactory.getDAO(sessionFactory, RoleUsersDAO.class);
        roleModuleOperateDAO = (RoleModuleOperateDAO) DAOFactory.getDAO(sessionFactory, RoleModuleOperateDAO.class);
        roleModuleFieldDAO = (RoleModuleFieldDAO) DAOFactory.getDAO(sessionFactory, RoleModuleFieldDAO.class);
        roleModuleUsersDAO = (RoleModuleUsersDAO) DAOFactory.getDAO(sessionFactory, RoleModuleUsersDAO.class);
        roleModuleDepartmentsDAO = (RoleModuleDepartmentsDAO) DAOFactory.getDAO(sessionFactory, RoleModuleDepartmentsDAO.class);
        scopeDataVisibleDAO = (ScopeDataVisibleDAO) DAOFactory.getDAO(sessionFactory, ScopeDataVisibleDAO.class);
    }
    
    /**
     * 刷新用户权限缓存
     * 
     * @param userId 用户ID
     * @throws CRUDException
     */
    public void flushRoleCache(String userId) throws CRUDException {
        List<RoleUsers> ruList = roleUsersDAO.findUsersRoleByUsers(userId);
        //清空缓存
        CacheUtil.put(RoleCache.CACHE_NAME, userId, null);
        RoleCache roleCache = new RoleCache();
        roleCache.setOperate(roleModuleOperateDAO.findUsersOperates(ruList, userId));
        roleCache.setModuleUsers(roleModuleUsersDAO.findRoleModuleUsers(ruList, userId));
        roleCache.setModuleDepartments(roleModuleDepartmentsDAO.findRoleModuleDepartments(ruList, userId));
        roleCache.setModuleFields(roleModuleFieldDAO.findUsersFields(ruList, userId));
        roleCache.setOtherOperates(scopeDataVisibleDAO.findOtherUsersOperates(ruList, userId));
        CacheUtil.put(RoleCache.CACHE_NAME, userId, roleCache);
    }

    /**
     * 刷新用户权限缓存
     * 
     * @param roleId 角色ID
     * @throws CRUDException
     */
    public void updateRoleCache(String roleId) throws CRUDException {
    	List<RoleUsers> list = roleUsersDAO.find(SQLConstant.ROLEUSERS_BY_ROLE_HQL, roleId);
    	if (Validate.collectionNotNull(list)) {
            for (Iterator iter = list.iterator(); iter.hasNext();) {
                RoleUsers ru = (RoleUsers) iter.next();
                flushRoleCache(ru.getUsers().getId());
            }
        }
    }
    
    /**
     * 获得用户权限缓存
     * 
     * @param usersId
     * @return
     */
    public RoleCache getUsersRoleCache(String usersId) {
        return (RoleCache) CacheUtil.get(RoleCache.CACHE_NAME, usersId);
    }

}